<template>
  <div class="container">
    <div class="search">
      <el-form :inline="true"  label-width="68px" size="small">
         <el-form-item label="角色名">
            <el-input v-model="searchData.name" placeholder="角色名"></el-input>
         </el-form-item>
      </el-form>
    </div>
    <div class="operation">
       <el-button type="primary" @click="search">搜索</el-button>
       <el-button type="primary" @click="handleAddRoot">添加</el-button>
    </div>
    <el-table :data="listData" style="width: 100%" :height="578" :tree-props="{children: 'children'}" row-key="id">
      <el-table-column prop="name" label="角色名">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleAddChild(scope.row)">添加子分类</el-button>
          <el-button size="mini" type="danger" @click="handleDeleteClassification(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="添加分类" width="30%" :visible="dialog == 'addClassification'" :before-close="handleAddClose">
      <el-form :inline="false" label-position="left" :model="addData" :rules="addClassificationRules" ref="addClassification" label-width="68px" size="small" >
        <el-form-item label="父级分类" prop="name">
          <el-input v-model="addData.parentName" placeholder="父级分类" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="分类名" prop="name">
          <el-input v-model="addData.name" ref="classificationNameInput"  placeholder="分类名"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog = '';clearAddClassificationData()">取 消</el-button>
        <el-button type="primary" @click="addClassification">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import api from '@/config/server.js'
export default {
  data () {
    return {
      listData: [],
      dialog: '',
      searchData: {
        name: ''
      },
      addData: {
        parentId: null,
        parentName: '',
        name: ''
      },
      page: {
        pageNumber: 1,
        pageSize: 10,
        total: 0,
        totalPage: 0
      },
      addClassificationRules: {}
    }
  },
  created () {
    this.search()
  },
  methods: {
    search () {
      api.commodityClassficationList({
      }).then((res) => {
        this.listData = res.result
      })
    },
    handleAddRoot () {
      this.dialog = 'addClassification'
      this.clearAddClassificationData()
    },
    addClassification () {
      api.addCommodityClassfication({
        name: this.addData.name,
        parentId: this.addData.parentId
      }).then((res) => {
        if (res.ret === 1) {
          this.$message('添加成功')
          this.clearAddClassificationData()
          this.dialog = ''
          this.search()
        } else {
          this.$message(res.msg)
        }
      })
    },
    handleAddClose () {
      this.dialog = ''
      this.clearAddClassificationData()
    },
    handleAddChild (row) {
      this.dialog = 'addClassification'
      this.addData = {
        parentId: row.id,
        parentName: row.name,
        name: ''
      }
      this.$nextTick(function () {
        this.$refs.classificationNameInput.focus()
      })
    },
    clearAddClassificationData () {
      this.addData = {
        parentId: null,
        parentName: '',
        name: ''
      }
    },
    handleDeleteClassification (row) {
      this.$confirm('确定删除该分类以及下属分类?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.deleteCommodityClassfication(row.id).then((res) => {
          if (res.ret === 1) {
            this.$message('删除成功')
            this.search()
          } else {
            this.$message(res.msg)
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .container{
    padding: 10px 0 0 0 ;
  }
  .search{
    .el-form{
      width:100%;
      display:flex;
      flex-direction:row;
      justify-content:flex-start;
      flex-wrap:wrap;
      .el-form-item{
        width: 270px;
      }
    }
    width:100%;
  }
  .operation{
    margin-top:10px;
    float: left;
  }
  .el-tree{
    width:200px;
  }
  // .el-input {
  //   width: 200px;
  // }

</style>>
